<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/1/13
 * Time: 13:35
 */

namespace app\api\service;


use app\common\service\ServerUpdate;

class ServerMiddleware extends Base
{
    /**
     * @name 注册
     * @param $data
     * @return string
     * @author wx
     * @date 2020/1/13 15:08
     */
    function register($data)
    {
        $data['ip'] = $_SERVER['REMOTE_ADDR'];

        /*$data['name'] = 123;
        $data['type'] = 'proxy';
        $data['version'] = '3.3.3.3';
        $data['desc'] = '这是3.3.3.3版本';
        $data['is_module'] = 0;*/
        $params = array('name','ip','type','version','desc','is_module');

        foreach ($params as $k => $v){
            if(!isset($data[$v])){
                $this->setError($this->CODE_FAIL,$v.' is must params');
                return 'fail';
            }
        }


        $server = new \app\common\service\ServerMiddleware();
        $condition['ip'] = $data['ip'];
        $condition['name'] = $data['name'];
        $condition['type'] = $data['type'];
        $info = $server->findData($condition);
        if($info){
            //更新
            $server->modify(['id' => $info['id']], $data);

        }else{
            $data['ctime'] = time();
            $server->add($data);
        }

        return 'success';
    }

    /**
     * @name
     * @param $data
     * @return array|bool|string
     * @author wx
     * @date 2020/1/13 15:01
     */
    function getPackagePath($data)
    {


        //1. 检查同时下载客户端数量
        $updateServer = new ProductUpdate();
        $online = $updateServer->online(20);
        if(!$online){
            $this->setError($this->CODE_FAIL, '同时下载数量已达上限,请等待!');
            return false;
        }
        $data['type'] = 'middleware';

        if (!isset($data['type']) || $data['type'] != 'middleware') {

            $this->setError($this->CODE_FAIL, 'params error');
            return false;
        }

        //查询更新包数据
        $list = db('server_update_list')->where('type', 'middleware')->select();

        $urlHeader = 'http://'.$_SERVER['HTTP_HOST'].'/';//协议头

        $jsonArr = array();
        foreach ($list as $k => $v){
            $v['url'] = substr($v['url'], strpos($v['url'],'public/') + strlen('public/'));
            if (file_exists($v['url'])) {
                $jsonArr[$k]['hash'] = $v['hash'];
                $jsonArr[$k]['url'] = $urlHeader.$v['url'];
                $jsonArr[$k]['name'] = $v['name'];
                $jsonArr[$k]['version'] = $v['version'];
                $jsonArr[$k]['desc'] = $v['desc'];
            }
        }

        if(empty($jsonArr)){
            $this->setError($this->CODE_FAIL, '当前已是最新版本');
            return 'fail';
        }

        //3. 添加数据到系统更新表中
        $ip = $_SERVER['REMOTE_ADDR'];//客户端ip

        $server = new ServerUpdate();
        $data_info = $server->findData(['ip' => $ip]);
        if($data_info){
            $updateData = array(
                'status' => 1,
                'last_time' => time(),
            );
            $server->modify(['id' => $data_info['id']], $updateData);
        }else{
            $addData = array(
                'ip' => $ip,
                'status' => 1,
                'last_time' => time(),
                'ctime' => time(),
            );
            $server->add($addData);
        }

        //4. 返回数据
        return $jsonArr;
    }
}